import { _decorator } from 'cc';

export default class BasicOpe {

    private _opeId: number = -1;
    private _opeName: string = "basic_ope"
    private _beginTime: number = -999999;

    //是否正在执行一个操作
    private static _curExecutingOpe: BasicOpe = null;

    constructor(opeName: string) {
        this._opeName = opeName;
        this._opeId = new Date().getTime();
        this._beginTime = -999999;
    }

    public get opeName() {
        return this._opeName;
    }

    public get opeId() {
        return this._opeId;
    }

    public get beginTime() {
        return this._beginTime;
    }

    public beginExecute() {
        this._beginTime = new Date().getTime();

        BasicOpe._curExecutingOpe = this;
    }

    public endExecute(): boolean {
        if (BasicOpe._curExecutingOpe != null) {
            if (BasicOpe._curExecutingOpe.opeId != this._opeId) {
                return false; //正在运行的ope和结束的ope的id不一致
            }
        }

        BasicOpe._curExecutingOpe = null;
        return true;
    }

    public static isExecutingCardOpe() {
        if (BasicOpe._curExecutingOpe == null) {
            return false;
        }

        let curTime = new Date().getTime();
        let timeOffset = curTime - BasicOpe._curExecutingOpe.beginTime;
        if (timeOffset >= 0 && timeOffset < 5000) {
            return true; //上一个ope超时
        }

        return false;
    }

}
